<template>
	<view class="content">
		<view class="content-top">
			{{detail.name}}
			<view>鸭鸭卡 {{detail.source}}</view>
		</view>
		<view class="title">任务描述</view>
		<view class="rwyq" v-html="detail.content">

		</view>
		<view class="submit" @click="open">提交任务</view>
		<uni-popup ref="popup">
			<view class="popup-cont">
				<view class="title2">任务完成说明</view>
				<view class="select">
					<view>
						<uni-data-select :clear="false" v-model="form.qy" :localdata="quList" @change="change1"
							placeholder="所属区域"></uni-data-select>
					</view>
					<view>
						<uni-data-select :clear="false" v-model="form.md" :localdata="mdList"
							placeholder="所属门店"></uni-data-select>
					</view>

				</view>
				<view class="input">
					<uni-easyinput type="textarea" v-model="form.content" placeholder="请输入任务完成情况文字说明"></uni-easyinput>
				</view>
				<view>
					<uni-file-picker :image-styles="imageStyles" limit="9" :value="fileLists" @select="select"
						title="请上传任务完成证明图片"></uni-file-picker>
				</view>
				<view style="height: 140rpx;"></view>
				<view class="submit2" @click="submit">提交任务</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getAction,
		postAction,
		uploadAction,
	} from '../../../common/request';
	export default {
		data() {
			return {
				form: {
					qy: '',
					md: '',
					content: '',
					pci: ''
				},
				value: 0,
				fileLists: [],
				detail: {},
				quList: [],
				mdList: [],
				imageStyles: {
					width: 88,
					height: 88,
				},
				taskId: ''
			};
		},
		onLoad(option) {
			this.taskId = option.id
			getAction('/task/show', {
				id: option.id
			}).then(res => {
				this.detail = res.data
			})
			getAction('/addr', {
				id: option.id
			}).then(res => {
				this.quList = res.data.map(i => {
					return {
						value: i.id,
						text: i.name,
						children: i.children.map(j => {
							return {
								value: j.id,
								text: j.name
							}
						})
					}
				})
			})
		},
		methods: {
			select(e) {
				uploadAction('/upload', {
					filePath: e.tempFilePaths[0]
				}).then(res => {
					this.fileLists.push({
						url: this.fileUrl + res.data,
						extname: res.data.substring(res.data.lastIndexOf('.') + 1, res.data.length),
						name: res.data.substring(res.data.lastIndexOf('/') + 1, res.data.length),
						path: res.data
					})

				})
			},
			change1(e) {
				const item = this.quList.find(i => i.value === e) || {
					children: []
				}

				this.mdList = item.children
				this.form.md = ''
			},
			open() {

				this.$refs.popup.open('bottom')
			},
			submit() {
				if (!this.form.qy) return uni.showToast({
					title: '请选择区域',
					icon: 'none'
				})
				if (!this.form.md) return uni.showToast({
					title: '请选择门店',
					icon: 'none'
				})
				if (!this.form.content) return uni.showToast({
					title: '请输入内容',
					icon: 'none'
				})
				const pic = this.fileLists.map(i => i.path)
				if (pic.length === 0) return uni.showToast({
					title: '请上传图片',
					icon: 'none'
				})
				uni.showLoading({
					mask: true
				})
				postAction('/user/task', {
					task_id: this.taskId,
					content: this.form.content,
					pic: pic.toString(),
					addr_id: this.form.md
				}).then(res => {
					uni.hideLoading()
					if (res.code === 0) {
						this.$refs.popup.close()
						uni.showToast({
							title: '提交成功'
						})
					} else {
						uni.showToast({
							title: res.msg
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 30rpx;
		padding-bottom: 150rpx;
		min-height: 100vh;

		.content-top {
			min-height: 200rpx;
			font-size: 36rpx;
			font-weight: 400;
			color: #2B2B2B;
			line-height: 44rpx;
			padding-bottom: 70rpx;
			position: relative;
			border-bottom: 2rpx solid #E0E0E0;

			>view {
				position: absolute;
				right: 0;
				bottom: 20rpx;
				color: #FF5035;
				line-height: 38rpx;
				text-align: right;
			}
		}

		.title {

			font-size: 40rpx;
			color: #EC7D02;
			line-height: 48rpx;
			margin-top: 22rpx;
			margin-bottom: 88rpx;
		}

		.rwyq {

			font-size: 28rpx;
			line-height: 34rpx;
			color: #4F4F4F;
		}

		.submit {
			position: fixed;
			left: 0;
			bottom: 0;
			right: 0;
			width: 100%;
			height: 100rpx;
			background: #FF5035;
			font-size: 32rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 100rpx;
		}
	}

	.popup-cont {
		background: #FFF;
		padding: 30rpx;
		border-radius: 30rpx 30rpx 0 0;
		min-height: 60vh;
		max-height: 100vh;
		overflow: auto;

		.title2 {
			line-height: 100rpx;
		}

		.select {
			height: 68rpx;
			margin-bottom: 40rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			>view {
				width: 48%;
				flex: inherit;
			}
		}

		.input {
			height: 200rpx;
		}

		.submit2 {
			position: absolute;
			bottom: 30rpx;
			left: 30rpx;
			width: 690rpx;
			height: 100rpx;
			background: #FF5035;
			font-size: 32rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 100rpx;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			margin-top: 50rpx;
		}
	}
</style>